package logic

import (
	"context"
	"github.com/jinzhu/copier"
	"go-zero-demo/common/tools"
	"go-zero-demo/user-api/excption"
	"go-zero-demo/user-rpc/internal/dao"

	"go-zero-demo/user-rpc/internal/svc"
	"go-zero-demo/user-rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserInfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoLogic {
	return &UserInfoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserInfoLogic) UserInfo(req *pb.UserInfoReq) (*pb.UserInfoResp, error) {
	logx.Infof("user info request:%s", tools.BeanToJson(req))
	info, queryErr := dao.GetUserInfo(l.svcCtx.DBEngin, req.UserId)
	if queryErr != nil {
		return nil, queryErr
	}
	if info.ID <= 0 {
		return nil, excption.NewErrCode(excption.UserNoExistError)
	}
	// 2.setting return obj...
	returnObj := new(pb.UserInfoResp)
	_ = copier.Copy(returnObj, info)
	returnObj.UserId = int32(info.ID)
	returnObj.UserName = info.NickName
	returnObj.Sex = info.Gender
	returnObj.Birthday = info.Birthday.Format("2006-01-02 15:04:05")
	returnObj.CreateTime = info.CreateTime.Format("2006-01-02 15:04:05")
	returnObj.UpdateTime = info.UpdateTime.Format("2006-01-02 15:04:05")
	return returnObj, nil
}
